// Counter 模块 - 演示基本的 state, mutations, actions, getters

export default {
  namespaced: true, // 启用命名空间
  
  state: {
    count: 0,
    history: []
  },
  
  getters: {
    // 计算属性：双倍计数
    doubleCount: (state) => state.count * 2,
    
    // 计算属性：是否为偶数
    isEven: (state) => state.count % 2 === 0,
    
    // 计算属性：历史记录数量
    historyCount: (state) => state.history.length
  },
  
  mutations: {
    // 增加计数
    INCREMENT(state, payload = 1) {
      state.count += payload
      state.history.push({
        type: 'increment',
        value: payload,
        timestamp: new Date().toLocaleTimeString()
      })
    },
    
    // 减少计数
    DECREMENT(state, payload = 1) {
      state.count -= payload
      state.history.push({
        type: 'decrement',
        value: payload,
        timestamp: new Date().toLocaleTimeString()
      })
    },
    
    // 重置计数
    RESET(state) {
      state.count = 0
      state.history.push({
        type: 'reset',
        value: 0,
        timestamp: new Date().toLocaleTimeString()
      })
    },
    
    // 设置计数
    SET_COUNT(state, value) {
      state.count = value
      state.history.push({
        type: 'set',
        value: value,
        timestamp: new Date().toLocaleTimeString()
      })
    }
  },
  
  actions: {
    // 异步增加计数
    incrementAsync({ commit }, payload = 1) {
      return new Promise((resolve) => {
        setTimeout(() => {
          commit('INCREMENT', payload)
          resolve()
        }, 1000)
      })
    },
    
    // 异步减少计数
    decrementAsync({ commit }, payload = 1) {
      return new Promise((resolve) => {
        setTimeout(() => {
          commit('DECREMENT', payload)
          resolve()
        }, 1000)
      })
    },
    
    // 批量操作
    batchIncrement({ commit }, times) {
      for (let i = 0; i < times; i++) {
        commit('INCREMENT', 1)
      }
    }
  }
}

